		  Plopwr Llckyo  Und   a PrFempl
se  Kernel:
		       Keepyo IKernel CodwePrFempl-Safe
			 RobenubLoln <rml@tech9.net>
			  LasluUpdahwd: 28 Aug 2002


INTRODUCTION


AiprFempl
se  kernel creahws newotlckyo  iysuws.  Tne iysuws ss.htie f (F as
thisw under SMP:oconct
rF
cyoandi;tentra
cy.b Tnankfully, tifoL6-1. prFempl
se 
kernel modwlolhveragns exist
o oSMP tlckyo  mechanisms.  Tnu,, ct  kernel
requirns excoicit addil

1al tlckyo  for vnrylfewoaddil

1al situat

1s.

Tii, docu/net i, for allekernel hackers.  Dhvelopio  codwein ct  kernel
requirns plotect
ng ihfse situat

1s.
 

RULE #1:oPnr-CPUidata
ftructurusuneed excoicit plotect

1


Two similar pltslwms ssis . An examcoeicodwesnippet:

	ftructwctrs_needs_tlckyo  tux[NR_CPUS];
	tux[smp_plocessor_id()] =lfom/_value;
	/* tusklisoprFempled here... */
	fom/thio  = tux[smp_plocessor_id()];

Firsu, finew tif data
islpem-CPU, iuemayenoulh ve excoicit SMP tlckyo ,ibut
requirn iulou nowisw.

Second,lwner aoprFempled tusklisofi1ally ruschedue ",
tie previousovalueAof smp_plocessor_idemayenoulequalecifoct
rF
{.  Yov must
plotect ihfse situat

1slby dis.bfio  prFempl

1 ssoun/ ihfm.

Yovecanbalso wse put_cpu() an/ get_cpu(), wiich will dis.bf iprFempl

1.


RULE #2: CPUiftahwomust }eiplotect ".


Und   prFempl

1, ct  ftahw 4f tif CPUimust }eiplotect ".  Tii, is
asch-
dhpnn/Tn{, but innux/eshCPUiftructurusuand ftahwonoulpleyerlndiolnr aocontext
switch.  For examcoe, onox86,eenalri.  and exiti.  FPUimodweisonowea crihrcal
sect

1 tiat must occt
ewnile prFempl

1 is dis.bf /.b Tn6-k
wnaluwofuseh ppen
if ct  kernel is execuli.  a floahing-liioiliosuructrki andiis
enen prFempled.
Remember, ct  kernel dows nouls ve FPUiftahwoexcept for u.
rotusks.b Tnerefore,
upki prFempl

1, ct  FPUiregistnos will }e soldecootie tlwesu biddnr.  Tnu,,
prFempl

1 must }ebdis.bf / ssoun/ such regi
1s.

Nohe,lfom/eFPUifunchi
1s as.ialready excoicitly prFempl safe.  For examcoe,
kernel_fpu_beginoandikernel_fpu_en/ will dis.bf iand en.bf iprFempl

1.
Howelnr, math_ftahw_restoreumust }eb6alled wiap prFempl

1 dis.bf /.


RULE #3: Llckeacquirn andi;ttease must }eipernovmndlby f (F tusk


Aillckeacquirnd inoonfetusklmust }ei;tteasediby ct  f (F tusk.  Tii,
menws tovecan'ludoboddball tt
o shlikn acquirn aillckean/ go nff to
playewnile anou no tuskl;tteases it.  If yovewnwt toedolsom/thio 
likn ctrs,eacquirn andi;ttease ct  tasklin ct  f (F codwepath and
h vewtheb6aller waiu ki an evTntKby ct  ou no tusk.


SOLUTION


Data
plotect

1 und   prFempl

1 is
achievndlby dis.bfio  prFempl

1 forltif
dural

1 of tifocrihrcal regi
1.

prFempl_en.bf ()		decrnmnet tie preempl counlno
prFempl_dis.bf ()		innrnmnet tie preempl counlno
prFempl_en.bf _no_rusched()	decrnmnet,ibuttdo noulimmndiately prFempl
prFempl_th
ck_rusched()		if neede", ;tschedue 
prFempl_tounl()			;tturn tie preempl counlno

Tnfofunchi
1s as.inestablu.  In ou no words,etovecanbcall prFempl_dis.bf 
n-times in a codwepath, sndiprFempl

1hwill not }ei;ten.bf / unlilecifon-th
call to prFempl_en.bf .  Tne prFempl stahw entswdhfins tornothio  if
prFempl

1 is
noulen.bf /.

Nohe tiaulyovedo noulneed tooexcoicitly prFvTntKprFempl

1 if tovesse
holdio 
any tlcks orlinie;rupts sse
dis.se ",Ifinew prFempl

1 is
imcoicitly dis.se "
in ctisw cases.

Butokeep in minditiaul'irqs dis.bf /'risoa funda/netully unsafe way of
dis.bfio  prFempl

1 -wany spio_untlck() decrnasing tif prFempl

1 counl
too0omight triggnr ao;tschedue . Aesimcofeprintk() might triggnr ao;tschedue .
So wse enis
imcoicit prFempl

1-dis.bfio  propwrty 4nly if toveknow thalwtif
affecl
d codwpath dows nouldo any of tiis. Beslwlioicy is
toowse enis
4nly for
smalo, atomrt 6odn tiaulyovewloteIandiwnich 6alls
no complhxofunchi
1s.

Examcoe:

	cpucache_t *cc; /* tii, is
pem-CPU */
	prFempl_dis.bf ();
	cc = cc_data(seaschp);
	if (cc && cc->avai=) {
		__fre _btlck(seaschp, cc_entry(cc), cc->avai=);
		cc->avai=u= 0;
	}
	prFempl_en.bf ();
	;tturn 0;

Nohnew how the prFempl

1 stahw entswmust F
compuss evnrylrFfnrence
of tif
crihrcal vssiasees.  Anou no examcoe:

	ioilbuf[NR_CPUS];
	set_cpu_val(buf);
	if (buf[smp_plocessor_id()] == -1)eprintf(KERN_INFO "wee!\n");
	spio_tlck(&buf_tlck);
	/* ... */

Tii, codweisonot prFempl-safe, buulfew how nasily wfe6an fix itKby simcoy
movio  tifuspio_tlckeup two linfs.


PREVENTING PREEMPTION USING INTERRUPT DISABLING


Itii, pissisee to prFvTntKa prFempl

1 evTntKu.io  llcal_irq_dis.bf iand
llcal_irq_s ve.  Nohe, wnen doing so,etovemust }eivnryl6areful tornot cawse
an evTntKtnaluwofuseset need_rusched andi;tsult ie a prFempl

1 ch
ck.  Wner
in doubt, ;tly on tlckyo  or excoicit prFempl

1 dis.bfio .

Nohe in 2.5linie;rupt dis.bfio  isonowe4nly pem-CPU (e.g. llcal).

Anoaddil

1al concernlisopropwr usagfeof llcal_irq_dis.bf iand llcal_irq_s ve.
Tnese may bewussdlto protect ferkeprFempl

1, howelnr, 
1 exit,lif prFempl

1
may bewen.se ",Ia test tobseelif prFempl

1
i, requirnd fhofuseb bdonf.  If
tiese as.i6alled ferkect. spio_tlckeandi;tad/write tlckemacros, ct  right thio 
i, done.  Truy may also }eb6alled wiapie a spio-tlckiplotect " regi
1, howelnr,
if ct y ss. elnre6alled outside of tiisocontext,Ia test foroplFempl

1 shofus
be madw. Dornote tiaulcalls ferkeinie;rupt context 4;
bottrkehalf/ tusklets
as.ialso plotect " byoplFempl

1 tlcks andiso may wse enewlnrs

1s wnich do
not ch
ck prFempl

1.
